package com.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.mapper.DishFlavorMapper;
import com.example.mapper.DishMapper;
import com.example.service.DishService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.dto.DishDTO;
import com.pojo.dto.DishFlavorDTO;
import com.pojo.dto.FlavorValueDTO;
import com.pojo.dto.QueryPageDTO;
import com.pojo.entity.Dish;
import com.pojo.entity.DishFlavor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired()
    private DishFlavorMapper dishFlavorMapper;

    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtil.copyProperties(dishDTO,dish);
        dish.setStatus(1);
        dishMapper.save(dish);
        Long dishId = dishMapper.getDishByName(dish.getName());
        List<DishFlavorDTO> flavors = dishDTO.getFlavors();
        for (DishFlavorDTO flavor:flavors){
            DishFlavor dishFlavor = new DishFlavor();
            dishFlavor.setDishId(dishId);
            dishFlavor.setName(flavor.getName());
            List<FlavorValueDTO> value = flavor.getValue();
            List<String> stringList = new ArrayList<>();
            for (FlavorValueDTO flavorValueDTO:value){
                String nameValue = flavorValueDTO.getNameValue();
                stringList.add(nameValue);
            }
            dishFlavor.setValue(stringList.toString());
            dishFlavorMapper.save(dishFlavor);
        }
    }

    @Override
    public Page<Dish> page(QueryPageDTO queryPageDTO) {
        PageHelper.startPage(queryPageDTO.getPageNum(),queryPageDTO.getPageSize());
        Page<Dish> dishPage = dishMapper.page(queryPageDTO);
        return dishPage;
    }

    @Override
    @Transactional
    public void deleteArr(Long[] ids) {
        dishMapper.deleteArr(ids);
        dishFlavorMapper.deleteArr(ids);
    }

    @Override
    @Transactional
    public DishDTO getDishAndFlavorById(Long id) {
        DishDTO dishDTO = new DishDTO();
        Dish dish = dishMapper.getDishById(id);
        BeanUtil.copyProperties(dish,dishDTO);
        List<DishFlavor> flavorList = dishFlavorMapper.getFlavorListByDishId(id);
        List<DishFlavorDTO> dishFlavorDTOList = new ArrayList<>();
        for (DishFlavor dishFlavor:flavorList){
            DishFlavorDTO dishFlavorDTO = new DishFlavorDTO();
            List<FlavorValueDTO> list = new ArrayList<>();
            String[] split = dishFlavor.getValue().substring(1, dishFlavor.getValue().length() - 1).split(",");
            for (String s:split){
                FlavorValueDTO flavorValueDTO = new FlavorValueDTO();
                flavorValueDTO.setNameValue(s);
                list.add(flavorValueDTO);
            }
            //BeanUtil.copyProperties(dishFlavor,dishFlavorDTO);
            dishFlavorDTO.setName(dishFlavor.getName());
            dishFlavorDTO.setId(dishFlavor.getId());
            dishFlavorDTO.setDishId(dishFlavor.getDishId());
            dishFlavorDTO.setValue(list);
            dishFlavorDTOList.add(dishFlavorDTO);
        }
        dishDTO.setFlavors(dishFlavorDTOList);
        return dishDTO;
    }

    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtil.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        List<DishFlavorDTO> flavors = dishDTO.getFlavors();
        List<String> list = new ArrayList<>();
        DishFlavor dishFlavor = new DishFlavor();
        //判断数据库有没有多的口味有则删除（员工进行了删除口味操作）
        List<DishFlavor> flavorListByDishId = dishFlavorMapper.getFlavorListByDishId(dishDTO.getId());
        int b = 0;
        for (DishFlavor dishFlavor1:flavorListByDishId){
            for (int i=0;i<flavors.size();i++){
                if (dishFlavor1.getId()==flavors.get(i).getId()){
                    b = 1;
                    break;
                }
            }
            if (b==0){
                dishFlavorMapper.deleteById(dishFlavor1.getId());
            }
            b=0;
        }
        for (DishFlavorDTO flavorDTO:flavors){
            DishFlavor dishFlavorById = dishFlavorMapper.getDishFlavorById(flavorDTO.getId());
            List<FlavorValueDTO> value = flavorDTO.getValue();
            if (dishFlavorById==null){
                //如果口味不存在则添加
                for (FlavorValueDTO flavorValueDTO:value){
                    list.add(flavorValueDTO.getNameValue());
                }
                dishFlavor.setValue(list.toString());
                list.clear();
                dishFlavor.setName(flavorDTO.getName());
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.save(dishFlavor);
            }else{
                //如果口味存在则修改
                for (FlavorValueDTO flavorValueDTO:value){
                    list.add(flavorValueDTO.getNameValue());
                }
                dishFlavor.setValue(list.toString());
                list.clear();
                dishFlavor.setName(flavorDTO.getName());
                dishFlavor.setDishId(flavorDTO.getDishId());
                dishFlavor.setId(flavorDTO.getId());
                dishFlavorMapper.update(dishFlavor);
            }
        }

    }
}
